
package com.dyzx.hbb.security.controller;

import com.dyzx.hbb.common.base.Result;
import com.dyzx.hbb.security.model.LoginUser;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/test")
public class TestAuthController {

    @GetMapping("/authenticated")
    public Result<Object> testAuthenticated() {
        Authentication auth = SecurityContextHolder.getContext().getAuthentication();
        LoginUser loginUser = (LoginUser) auth.getPrincipal();
        
Map<String, Object> data = new HashMap<>();
            data.put("userId", loginUser.getId());
            data.put("username", loginUser.getUsername());
            data.put("authorities", loginUser.getAuthorities());
            data.put("isAuthenticated", auth.isAuthenticated());
            data.put("authentication", auth);
            data.put("permissions", loginUser.getPermissions());
            return Result.ok(data);

    }

    @GetMapping("/admin")
    public Result<String> testAdmin() {
        return Result.ok("You have ADMIN access");
    }

    @GetMapping("/user")
    public Result<String> testUser() {
        return Result.ok("You have USER access");
    }
}